﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalProductInshop
    {
        //Cap nhat thong tin san pham trong Shop
        public static int updateProductInShop(BizProductInShop pProductInShop)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var productInShop = db.ProductInShop.Single(p => p.ByProduct == pProductInShop.by_product && p.ByShop == pProductInShop.by_shop);
                productInShop.Quantity = pProductInShop.quantity;
                productInShop.PriceInShop = pProductInShop.price_in_shop;
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        //Cap nhat so luong cua mot san pham trong Shop
        public static int updateProductQuantityInShop(string pBy_product, string pBy_shop, int pQuantity)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var productInShop = db.ProductInShop.Single(p => p.ByProduct == pBy_product && p.ByShop == pBy_shop);
                productInShop.Quantity += pQuantity;
                db.SubmitChanges();
                return 1;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        //Lay so luong hien tai cua mot san pham trong Shop
        public static int getProductQuantityInShop(string pBy_product, string pBy_shop)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var productInshop = db.ProductInShop.Single(p => p.ByProduct == pBy_product && p.ByShop == pBy_shop);
                return productInshop.Quantity;
            }
            catch(Exception ex)
            {
                return 0;
            }
        }

        //Cap nhat gia san pham tai cua hang
        public static int updateProductPriceInShop(string pBy_product, string pBy_shop, decimal pPrice)
        {
            try
            {
                DalBase obj = new DalBase();
                string commandText = String.Format("update product_in_shop set price_in_shop = '{0}' where by_product = '{1}' and by_shop = '{2}'", pPrice, pBy_product, pBy_shop);
                obj.executeNonqueryCommand(commandText);
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
